package cn.com.blueInfo.bpm.system.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.system.entity.DictItem;
import cn.com.blueInfo.bpm.system.mapper.DictItemMapper;
import cn.com.blueInfo.bpm.system.repository.DictItemRepository;
import cn.com.blueInfo.bpm.system.request.DictItemDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 枚举项表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class DictItemRepositoryImpl
        extends ServiceImpl<DictItemMapper, DictItem>
        implements DictItemRepository, BaseRepository<DictItem, DictItemDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增枚举项
     * @param dictItem 枚举项参数
     */
    @Override
    public Integer createDictItem(DictItem dictItem) {
        return sqlExecuteResult(save(dictItem));
    }

    /**
     * 批量新增枚举项
     * @param dictItemList 枚举项参数列表
     */
    @Override
    public Integer batchCreateDictItem(List<DictItem> dictItemList) {
        return sqlExecuteResult(saveBatch(dictItemList), dictItemList.size());
    }

    /**
     * 删除枚举项
     * @param dictItemId 枚举项ID
     */
    @Override
    public Integer deleteDictItem(Long dictItemId) {
        DictItem dictItem = new DictItem();
        dictItem.setId(dictItemId);
        dictItem.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(dictItem));
    }

    /**
     * 批量删除枚举项
     * @param dictItemIdList 枚举项ID列表
     */
    @Override
    public Integer batchDeleteDictItem(List<Long> dictItemIdList) {
        List<DictItem> dictItemList = new ArrayList<>();
        for (Long dictItemId : dictItemIdList) {
            DictItem dictItem = new DictItem();
            dictItem.setId(dictItemId);
            dictItem.setDeleted(BaseDeleted.DELETED);
            dictItemList.add(dictItem);
        }
        return sqlExecuteResult(removeBatchByIds(dictItemList), dictItemList.size());
    }

    /**
     * 更新部分枚举项
     * @param dictItem 枚举项参数
     */
    @Override
    public Integer updateDictItemPartial(DictItem dictItem) {
        validateUpdateId(dictItem.getId());
        LambdaUpdateWrapper<DictItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DictItem::getId, dictItem.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部枚举项
     * @param dictItem 枚举项参数
     */
    @Override
    public Integer updateDictItem(DictItem dictItem) {
        validateUpdateId(dictItem.getId());
        return sqlExecuteResult(updateById(dictItem));
    }

    /**
     * 查询一条枚举项
     * @param dictItemId 枚举项ID
     */
    @Override
    public DictItem getDictItemById(Long dictItemId) {
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictItem::getId, dictItemId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询枚举项
     * @param dictItemQueryDTO 枚举项参数
     */
    @Override
    public Page<DictItem> getDictItemPage(DictItemDTO.QueryDTO dictItemQueryDTO) {
        Page<DictItem> dictItemPage = new Page<>(
                dictItemQueryDTO.getPage(), dictItemQueryDTO.getSize());
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dictItemQueryDTO);

        BaseDeleted deleted = dictItemQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(dictItemPage, queryWrapper);
        } else {
            return page(dictItemPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 枚举项 数量
     * @param dictItemQueryDTO 枚举项参数
     */
    @Override
    public Long getDictItemCount(DictItemDTO.QueryDTO dictItemQueryDTO) {
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dictItemQueryDTO);

        BaseDeleted deleted = dictItemQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 枚举项
     * @param dictItemQueryDTO 枚举项参数
     */
    @Override
    public List<DictItem> getDictItemList(DictItemDTO.QueryDTO dictItemQueryDTO) {
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dictItemQueryDTO);

        BaseDeleted deleted = dictItemQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<DictItem> queryWrapper, DictItemDTO.QueryDTO dictItemQueryDTO) {

        Long dictId = dictItemQueryDTO.getDictId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(dictId), DictItem::getDictId, dictId);

        String itemValue = dictItemQueryDTO.getItemValue();
        queryWrapper.eq(StringUtils.isNotBlank(itemValue), DictItem::getItemValue, itemValue);

        String itemLabel = dictItemQueryDTO.getItemLabel();
        queryWrapper.eq(StringUtils.isNotBlank(itemLabel), DictItem::getItemLabel, itemLabel);

        String itemColor = dictItemQueryDTO.getItemColor();
        queryWrapper.eq(StringUtils.isNotBlank(itemColor), DictItem::getItemColor, itemColor);

        String description = dictItemQueryDTO.getDescription();
        queryWrapper.eq(StringUtils.isNotBlank(description), DictItem::getDescription, description);

        addQueryWrapper(queryWrapper, dictItemQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<DictItem, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                DictItem::getOrderNumber,
                DictItem::getTenantId,
                DictItem::getApplicationId,
                DictItem::getStatus,
                DictItem::getDeleted,
                DictItem::getDeleteUserId,
                DictItem::getDeleteTime,
                DictItem::getRemark,
                DictItem::getCreateUserId,
                DictItem::getCreateTime,
                DictItem::getUpdateUserId,
                DictItem::getUpdateTime,
                DictItem::getExtend1,
                DictItem::getExtend2,
                DictItem::getExtend3);
    }

}
